module controller.role;

import std.conv : to;
import vibe.http.server : HTTPServerRequest, HTTPServerResponse;
import vibe.data.json : Json, deserializeJson;
import repository.role;
import models.role;
import dtos.role;
import utils.respdata;
import utils.objmapping;

import std.stdio : writeln;

/** 
 * 获取角色下拉列表
 */
void getRoleOptions(HTTPServerRequest req, HTTPServerResponse resp)
{
    auto allRole = getAllRoles();

    RoleOptionDto[] roleOptions;
    foreach (role; allRole)
    {
        roleOptions ~= new RoleOptionDto(role.id, role.name);
    }

    resp.writeJsonBody(RespData.newData!(RoleOptionDto[])(200, "ok", roleOptions));
}

/** 
 * 角色列表
 */
void getRoles(HTTPServerRequest req, HTTPServerResponse resp)
{
    import vibe.data.json : Json, serializeToJson;

    // 放弃处理分页
    auto allRole = getAllRoles();
    RoleDto[] roleDtos = [];
    foreach (role; allRole)
    {
        roleDtos ~= mappingToObject!(RoleDto, Role)(role);
    }

    Json jsonData = Json.emptyObject;
    jsonData["total"] = allRole.length;
    jsonData["list"] = serializeToJson(roleDtos);

    Json data = Json.emptyObject;
    data["code"] = 200;
    data["msg"] = "ok";
    data["data"] = jsonData;

    resp.writeJsonBody(data);
}

/** 
 * 角色菜单ids
 */
void getRoleMenuIds(HTTPServerRequest req, HTTPServerResponse resp)
{
    if ("roleid" !in req.params)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }
    auto role = getRoleById(to!long(req.params["roleid"]));

    long[] roleMenuIds = [];
    import std.algorithm : each;

    role.menus.each!(x => roleMenuIds ~= x.id);

    resp.writeJsonBody(RespData.newData!(long[])(200, "ok", roleMenuIds));
}

/** 
 * 为角色分配权限
 */
void roleAssignPerm(HTTPServerRequest req, HTTPServerResponse resp)
{
    if ("roleid" !in req.params)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }
    // 接收前端数据
    Json json = req.json;
    if (json.length == 0)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }

    auto roleId = to!long(req.params["roleid"]);
    long[] menuIds = deserializeJson!(long[])(json);

    bool res = doUpdateRoleMenu(roleId, menuIds);
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "操作失败"), 500);
        return;
    }

    resp.writeJsonBody(RespData.newData(200, "ok"), 200);
}

/**
* 添加角色
*/
void addRole(HTTPServerRequest req, HTTPServerResponse resp)
{
    // 接收前端数据
    Json json = req.json;
    if (json.length == 0)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }
    RoleDtoForCreate newRole = deserializeJson!(RoleDtoForCreate)(json);
    if (newRole.name == "" || newRole.code == "")
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }
    bool res = doAddRole(mappingToObject!(Role, RoleDtoForCreate)(newRole));
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "操作失败"), 500);
        return;
    }

    resp.writeJsonBody(RespData.newData(200, "ok"), 200);
}

/**
* 删除角色
*/
void deleteRole(HTTPServerRequest req, HTTPServerResponse resp)
{
    import std.array : array, split;
    import std.algorithm : map, canFind;

    if ("roleid" !in req.params)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }

    string reqRoleid = req.params["roleid"];
    bool res = false;
    // 删除单个
    if (!canFind(reqRoleid, ","))
    {
        long roleid = to!long(reqRoleid);
        res = deleteRoleById(roleid);
    }
    else
    {
        // 批量删除
        string[] tempids = reqRoleid.split(",");
        long[] roleids = map!(x => to!long(x))(tempids).array();
        res = deleteRoleByIds(roleids);
    }
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "操作失败"), 500);
        return;
    }
    resp.writeJsonBody(RespData.newData(200, "ok"));
}

/**
* 编辑时获取角色详情信息
*/
void getRoleInfo(HTTPServerRequest req, HTTPServerResponse resp)
{
    if ("roleid" !in req.params)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }

    auto role = getRoleById(to!long(req.params["roleid"]));
    RoleDto roleDto = mappingToObject!(RoleDto, Role)(role);

    resp.writeJsonBody(RespData.newData!(RoleDto)(200, "ok", roleDto));
}

/**
* 编辑角色
*/
void editRole(HTTPServerRequest req, HTTPServerResponse resp)
{
    if ("roleid" !in req.params)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }
    long reqRoleid = to!long(req.params["roleid"]);

    // 接收前端json数据
    Json json = req.json;
    if (json.length == 0)
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }
    auto upRole = deserializeJson!(RoleDtoForUpdate)(json);
    if (upRole.name == "" || upRole.code == "")
    {
        resp.writeJsonBody(RespData.newData(400, "错误的请求"), 400);
        return;
    }

    // 提交修改数据
    auto res = doUpdateRole(reqRoleid, mappingToObject!(Role, RoleDtoForUpdate)(upRole));
    if (!res)
    {
        resp.writeJsonBody(RespData.newData(500, "操作失败"), 500);
        return;
    }

    resp.writeJsonBody(RespData.newData(200, "ok"), 200);
}
